#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct XuanMin
{
    char name[32];
    int votes;

};

struct XuanMin *InitXm(struct XuanMin *p,int *pn)
{
    int i=0,j=0;
    if(p==NULL)
    {
        printf("请输入几人参选:\n");
        scanf("%d",pn);
        p=(struct XuanMin*)malloc((*pn)*sizeof(struct XuanMin));
    }

    for ( i = 0; i < *pn; i++)
    {
        p->votes=0;
        printf("请输入参选者名字:\n");
        scanf("%s",p->name);
        p++;
    }

    p=p-*pn;
    printf("参选者为:\n");
    for (j = 0; j < *pn; j++)
    {
        printf("%s ",p->name);
        p++;        
    }
    puts("\n");
   return  p-*pn;
}

int DoVote(struct XuanMin *p,int pn)
{   
    int i=0,j=0;
    int ticket=0;
    char xm[32];
    struct XuanMin *pback=p;
    
    for ( i = 0; i < pn+1; i++)
    {
        int mark=0;
        memset(xm,'\0',sizeof(xm));
        printf("请投票:\n");
        scanf("%s",&xm);
        p=pback;
        for ( j = 0; j < pn; j++)
        {
            if(strcmp(xm,p->name)==0)
            {
                mark=1;
                p->votes++;
            }
            p++;
        }
        if (mark==0)
        {
            ticket++;
        }      
    }
    return ticket;
}

void PrintVote(struct XuanMin *p,int pn,int ab)
{
    int i = 0;
    for ( i = 0; i < pn; i++)
    {
        printf("选民%s: %d票\n",p->name,p->votes);
        p++;   
    }
    printf("弃票为：%d\n",ab);
}


struct XuanMin *WinVote(struct XuanMin *p,int pn)
{
    int i=0;
    struct XuanMin *max;
    max=p;

    for ( i = 0; i < pn; i++)
    {
        if(max->votes<p->votes)
        {
            max->votes=p->votes;
        }     
        p++;
    }
    
    return max;
}

int main(){

    struct XuanMin *Xm=NULL;
    int total=0;
    int i=0,j=0;
    
    Xm=InitXm(Xm,&total);
    int Abandon=DoVote(Xm,total);
    PrintVote(Xm,total,Abandon);
    struct XuanMin *Final=WinVote(Xm,total);
    printf("最后%s以%d票赢得选举!!!\n",Final->name,Final->votes);
    return 0;
}